package com.uenpay.im.ui.old.activity.dynamic;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.FileUtils;
import com.qiyunxin.android.http.utils.Utils;
import com.qiyunxin.android.http.view.ResizeLayout;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.panel.FacePanelByEmojiManager;
import com.uenpay.im.panel.PanelManagerInterface;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * friends dynamic list
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class DynamicDetailActivity extends BaseActivity implements
        OnClickListener {

    private DynamicHandle mDynamicHandle = new DynamicHandle();
    private ListView mListView;
    private ArrayList<DynamicEntity> dynamics = new ArrayList<DynamicEntity>();
    private View loading;
    private int page = 1;
    private int replyDynamicId;
    private EditText editText;
    private View dynamic_pop_reply;
    protected FileUtils fileUtils = new FileUtils(IMApplication.appName);
    private DynamicAdapter adapter;
    private boolean is_restore;
    private int replySourceCustId;
    private String dynamicid;
    private Handler handler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 5:
                    if (page == 1) {
                        if (adapter != null) {
                            adapter.setData(dynamics);
                        } else {
                            initData();
                        }
                        mListView.setSelection(0);
                        page++;
                    } else {
                        ArrayList<DynamicEntity> arraylist = (ArrayList<DynamicEntity>) msg.obj;
                        if (arraylist != null && arraylist.size() > 0) {
                            adapter.addData(arraylist);
                            page++;
                        }
                    }
                    break;
                case 6:
                    if (loading != null) {
                        loading.setVisibility(View.GONE);
                    }
                    break;
                default:
                    break;
            }
            return false;
        }
    });
    private FacePanelByEmojiManager mFacePanelByEmojiManager;
    private ResizeLayout mResizeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_friend_dynamic_detail;
    }

    @Override
    public void initView(View view) {
        dynamicid = getIntent().getStringExtra("dynamic_id");
        loading = findViewById(R.id.dynamic_loading);
        loading.setVisibility(View.VISIBLE);
        initView();
        initListener();
        initFacePanelView();
        initData();
        getDynamic(dynamicid);
    }

    private void initFacePanelView() {
        mFacePanelByEmojiManager = new FacePanelByEmojiManager(this,
                dynamic_pop_reply, mResizeLayout);
        mFacePanelByEmojiManager.initListener();
        mFacePanelByEmojiManager.onSendTextListener(new PanelManagerInterface.ISendTextListener() {

            @Override
            public void onSendTextContent(String editContent) {
                if (!TextUtils.isEmpty(editContent.trim())) {
                    if (editContent.length() <= 140) {
                        Utils.hideSoftKeyboard(editText);
                        dynamic_pop_reply.setVisibility(View.GONE);
                        if (is_restore) {
                            replyDynamic(editContent);
                            is_restore = false;
                            editText.setHint("");
                        } else {
                            replyDynamic(editContent);
                        }

                    } else {
                        IMApplication.showToast(R.string.more_text_max);
                    }
                } else {
                    IMApplication.showToast(R.string.reply_empty);
                }
            }
        });
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.dynamic_detail);
        editText = (EditText) findViewById(R.id.editText);
        dynamic_pop_reply = findViewById(R.id.dynamic_pop_reply);
        mResizeLayout = (ResizeLayout) findViewById(R.id.parent);
        mListView = (ListView) findViewById(R.id.listview_dynamic);
    }

    protected void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(this);
        mListView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mFacePanelByEmojiManager != null) {
                    mFacePanelByEmojiManager.hideFacePanelView();
                }
                dynamic_pop_reply.setVisibility(View.GONE);
                return false;
            }
        });
    }

    public void initData() {
        adapter = new DynamicAdapter(editText, this, dynamics,
                new DynamicAdapter.IDynamicReplyListener() {

                    @Override
                    public void onDynamicReply(int dynamicId,
                                               int source_cust_id, int position) {
                        // 评论
                        replyDynamicId = dynamicId;
                        replySourceCustId = source_cust_id;
                        if (source_cust_id > 0) {
                            is_restore = true;
                        }
                        dynamic_pop_reply.setVisibility(View.VISIBLE);
                        if (mFacePanelByEmojiManager != null) {
                            mFacePanelByEmojiManager.showFacePanelView();
                            Utils.showSoftKeyboard(editText);
                        }
                        mListView.setSelection(mListView.getHeaderViewsCount()
                                + position);

                    }
                });
        mListView.setAdapter(adapter);
    }

    private void hideSoftKeyboard() {
        if (editText != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    public void getDynamic(String dynamicid) {
        mDynamicHandle.getDynamicDetail(dynamicid,
                new DynamicHandle.IGetDynamicListResultListener() {

                    @Override
                    public void onGetDynamicResult(int status, String msg,
                                                   ArrayList<DynamicEntity> resultDynamics) {
                        loading.setVisibility(View.GONE);
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (resultDynamics != null) {
                                if (resultDynamics.size() > 0) {
                                    dynamics = resultDynamics;
                                    Message message = handler.obtainMessage();
                                    message.what = 5;
                                    message.obj = resultDynamics;
                                    handler.sendMessage(message);
                                }
                            }
                        }
                    }
                });

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.back_layout) {
            hideSoftKeyboard();
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    finish();
                }
            }, 300);


        } else {
        }
    }

    /**
     * 评论动态
     *
     * @param content
     */
    private void replyDynamic(final String content) {
        loading.setVisibility(View.VISIBLE);
        mDynamicHandle.replyDynamic(replySourceCustId, replyDynamicId, content,
                new DynamicHandle.IReplyDynamicResultListener() {

                    @Override
                    public void onReplyDynamicReslut(int status, String msg,
                                                     Reply mReply) {
                        if (isFinishing())
                            return;
                        handler.sendEmptyMessage(6);
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            editText.setText("");
                            Utils.hideSoftKeyboard(editText);
                            /** 将评论的内容添加到本地列表**/
                            mReply.nickname = IMApplication.config
                                    .getUserName();
                            adapter.updateReply(mReply, replyDynamicId);
                        }
                    }
                });

    }

    @Override
    protected void onDestroy() {
        if (adapter != null) {
            adapter.RecycleMaskImageView(true);
        }
        IMApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }

}
